<?php

/***************************************************************************
 Jupiter Content System @ Jupiterportal.com
 Copyright (C) 2005 Cosmin Flavius (highstrike@gmail.com)

 This program is free software; you can redistribute it and/or
 modify it under the terms of the GNU General Public License
 as published by the Free Software Foundation; either version 2
 of the License, or (at your option) any later version.

 More Info About The Licence At http://www.gnu.org/copyleft/gpl.html
****************************************************************************/

//-----------------------------------------------------------------
//  Make function for search form
//-----------------------------------------------------------------

function searchForm()
{
	global $PHP_SELF, $language;

	//-----------------------------------------------------------------
	//  Remember the string and option
	//-----------------------------------------------------------------

	$searchwords = (isset($_POST['words']) ? htmlspecialchars(stripslashes($_REQUEST['words'])) : '');
	@$forum = (($_POST['loc'] == '1') ? ' selected="selected"' : '' );
	@$desc = (($_POST['loc'] == '2') ? ' selected="selected"' : '' );
	@$users = (($_POST['loc'] == '3') ? ' selected="selected"' : '' );

	//-----------------------------------------------------------------
	//  Output the form
	//-----------------------------------------------------------------

	?>
		<table class='main' cellspacing='1' cellpadding='4'>
		<col width="60%"><col width="30%"><col width="10%">
		<tr class='head'><td colspan='3'><?= $language['Search view desc'] ?></td></tr>
		<tr class='bottom'><td><?= $language['Search view desc2'] ?> | <a href='<?= $PHP_SELF ?>?n=modules/misc&a=5'><?= $language['Search view desc3'] ?></a></td><td colspan='3'><?= $language['Search view desc4'] ?></td></tr>
		<form method='post' action='?n=modules/search'>
		<tr class='con1'><td><input type="text" class="box" style="width:100%" name="words" value="<?= $searchwords ?>"></td>
		<td><select class="box" style="width:100%" name="loc"><option value="1" <?= $forum ?>><?= $language['Search view desc5'] ?></option><option value="2" <?= $desc ?>><?= $language['Search view desc6'] ?></option><option value="3" <?= $users ?>><?= $language['Search view desc7'] ?></option></select></td>
		<td><input class="box" style="width:100%" type="submit" value="<?= $language['Search view desc8'] ?>"></td></tr>
		<input type="hidden" name="a" value="1">
		</form>
		</table>
	<?
}

//-----------------------------------------------------------------
//  Start action switch
//-----------------------------------------------------------------

switch($a)
{
	//-----------------------------------------------------------------
	//  Do search
	//-----------------------------------------------------------------

	case 1:

		//-----------------------------------------------------------------
		//  Load the form
		//-----------------------------------------------------------------

		searchForm();

		//-----------------------------------------------------------------
		//  Take care of our string
		//-----------------------------------------------------------------

		$searchstring = mysql_escape_string($_POST['words']);

		//-----------------------------------------------------------------
		//  Start check
		//-----------------------------------------------------------------

		if(!$searchstring){ header("location: $PHP_SELF?n=modules/search&i=134"); exit; }

		//-----------------------------------------------------------------
		//  Start secondary action switch
		//-----------------------------------------------------------------

		switch($loc)
		{
			//-----------------------------------------------------------------
			//  Search in topic name
			//-----------------------------------------------------------------

			case 1:

				//-----------------------------------------------------------------
				//  Get the search results
				//-----------------------------------------------------------------

				$result = $db->getTable("SELECT t.id AS tid, t.mode, t.name, t.type, t.author_id, t.author_user, t.views, t.lastpost_date, t.author_email, t.lastpost_reg, t.lastpost_id, t.lastpost_user, t.lastpost_location, t.lastpost_email, s.type AS stype, COUNT(r.id) AS replies, r.cat, r.subcat, ua.id AS uaid, ul.id AS ulid, MATCH(t.name) AGAINST ('$searchstring' IN BOOLEAN MODE) AS score FROM forum_topics AS t LEFT JOIN forum_subcat AS s ON s.id=t.subcat LEFT JOIN forum_replies AS r ON r.topic=t.id LEFT JOIN users AS ua ON t.author_id=ua.id LEFT JOIN users AS ul ON t.lastpost_id=ul.id WHERE MATCH(t.name) AGAINST ('$searchstring' IN BOOLEAN MODE) GROUP BY t.id ORDER BY score DESC");

				//-----------------------------------------------------------------
				//  Do some HTML
				//-----------------------------------------------------------------

				echo "<br>";
				echo "<table class='main' cellspacing='1' cellpadding='4'>";
				echo "<tr class='head'><td colspan='6'>{$language['Search action desc']}</td></tr>";
				echo "<tr class='bottom'><td width='40%' colspan='2'>{$language['Forum view topics desc']}</td><td width='8%' align='center'>{$language['Forum view topics desc2']}</td><td width='14%' align='center'>{$language['Forum view topics desc3']}</td><td width='8%' align='center'>{$language['Forum view topics desc4']}</td><td width='30%'>{$language['Forum view topics desc5']}</td></tr>";

				//-----------------------------------------------------------------
				//  Check if we have results
				//-----------------------------------------------------------------

				if($result != FALSE)
				{
					//-----------------------------------------------------------------
					//  Do foreach
					//-----------------------------------------------------------------

					foreach($result as $idx=>$forum_topic)
					{
						//-----------------------------------------------------------------
						//  Filter search for topics in admin subcategory
						//-----------------------------------------------------------------

						if(isset($is_moderator) || isset($is_administrator) || isset($is_webmaster)){}
						else if($forum_topic['stype'] == "3") continue;

						//-----------------------------------------------------------------
						//  Check see what indicator fits
						//-----------------------------------------------------------------

						if($forum_topic['mode'] == "2") $forum_indicator = "<img src='templates/$template/images/buttons/sticky.gif' alt='{$language['Forum indicator desc17']}'>";
						if($forum_topic['mode'] == "3")
						{
							if(@in_array(array("{$forum_topic['tid']}","{$forum_topic['subcat']}","{$forum_topic['lastpost_date']}"),$_SESSION['topic']) && !@in_array(array("{$forum_topic['tid']}","{$forum_topic['subcat']}","{$forum_topic['lastpost_date']}"),$_SESSION['topic_read']))
							{
								if($forum_topic['type'] == "1")
								{
									if(($forum_topic['replies'] - 1) >= $config['forum_hot']) $forum_indicator = "<img src='templates/$template/images/buttons/hot_new.gif' alt='{$language['Forum indicator desc11']}'>";
									else $forum_indicator = "<img src='templates/$template/images/buttons/new.gif' alt='{$language['Forum indicator desc10']}'>";
								}
								if($forum_topic['type'] == "2") $forum_indicator = "<img src='templates/$template/images/buttons/locked_new.gif' alt='{$language['Forum indicator desc12']}'>";
							}
							else
							{
								if($forum_topic['type'] == "1")
								{
									if(($forum_topic['replies'] - 1) >= $config['forum_hot']) $forum_indicator = "<img src='templates/$template/images/buttons/hot_old.gif' alt='{$language['Forum indicator desc14']}'>";
									else $forum_indicator = "<img src='templates/$template/images/buttons/old.gif' alt='{$language['Forum indicator desc13']}'>";
								}
								if($forum_topic['type'] == "2") $forum_indicator = "<img src='templates/$template/images/buttons/locked_old.gif' alt='{$language['Forum indicator desc15']}'>";
							}

							if($forum_topic['type'] == "3") $forum_indicator = "<img src='templates/$template/images/buttons/moved.gif' alt='{$language['Forum indicator desc18']}'>";
						}

						//-----------------------------------------------------------------
						//  Make mini-navigation
						//-----------------------------------------------------------------

						$page = ceil($forum_topic['replies']/$config['nav_replies']);

						if($page > 1) $inner_pag = "[ ".paginate($page, 0, "$PHP_SELF?n=modules/forum&a=3&d={$forum_topic['cat']}&o={$forum_topic['subcat']}&q={$forum_topic['tid']}")." ]";
						else $inner_pag = NULL;

						//-----------------------------------------------------------------
						//  Do more HTML
						//-----------------------------------------------------------------

						echo "<tr class='con1'>";
						echo "<td class='con1' align='center' width='6%'>$forum_indicator</td>";
						echo "<td><a href='$PHP_SELF?n=modules/forum&a=3&d={$forum_topic['cat']}&o={$forum_topic['subcat']}&q={$forum_topic['tid']}'>".cuttext($forum_topic['name'],30)."</a><br>$inner_pag</td>";
						if($forum_topic['type'] == 3) echo "<td class='con2' align='center'>--</td>"; else echo "<td class='con2' align='center'>".($forum_topic['replies'] - 1)."</td>";

						if(isset($forum_topic['uaid'])) echo "<td class='con2' align='center'><a href='$PHP_SELF?n=modules/users&a=1&d={$forum_topic['author_id']}'>{$forum_topic['author_user']}</a></td>";
						else  echo "<td class='con2' align='center'><a href='mailto:{$forum_topic['author_email']}'>{$forum_topic['author_user']}</a></td>";

						if($forum_topic['type'] == 3) echo "<td class='con2' align='center'>--</td>"; else echo "<td class='con2' align='center'>{$forum_topic['views']}</td>";

						if($forum_topic['type'] == 3) echo "<td>{$language['Forum view topics desc8']} {$forum_topic['redirect_hits']}</td></tr>";
						else
						{
							echo "<td class='con2'><img src='templates/$template/images/buttons/date.gif' border='0' alt='{$language['Forum view topics desc6']}'> ".today_yesterday($forum_topic['lastpost_date'])."<br>{$language['Forum view topics desc7']} ";

							if(isset($forum_topic['ulid'])) echo "<a href='$PHP_SELF?n=modules/users&a=1&d={$forum_topic['lastpost_id']}'>{$forum_topic['lastpost_user']}</a>";
							else echo "<a href='mailto:{$forum_topic['lastpost_email']}'>{$forum_topic['lastpost_user']}</a>";

							echo " <a href='$PHP_SELF?n=modules/forum&a=6&d={$forum_topic['lastpost_location']}'><img src='templates/$template/images/buttons/last_post.gif' border='0' alt='{$language['Forum view topics desc5']}'></a></td></tr>";
						}
					}
				}

				//-----------------------------------------------------------------
				//  If we don't have results show error
				//-----------------------------------------------------------------

				else echo "<tr class='con1'><td colspan='6'>{$language['Search action desc']}</td></tr>";

				//-----------------------------------------------------------------
				//  Finish the HTML, close the table
				//-----------------------------------------------------------------

				echo "</table>";
			break;

			//-----------------------------------------------------------------
			//  Search in replies message
			//-----------------------------------------------------------------

			case 2:

				//-----------------------------------------------------------------
				//  Get the search results
				//-----------------------------------------------------------------

				$result = $db->getTable("SELECT r.id AS rid, r.user_id, r.user_reg, r.user_user, r.user_email, r.date, r.subcat, r.message, c.id AS cid, c.name AS cname, s.id AS sid, s.name AS sname, s.type, t.id AS tid, t.name AS tname, u.id AS uid, MATCH(r.message) AGAINST ('$searchstring' IN BOOLEAN MODE) AS score FROM forum_replies AS r LEFT JOIN forum_cat AS c ON r.cat=c.id LEFT JOIN forum_subcat AS s ON r.subcat=s.id LEFT JOIN forum_topics AS t ON r.topic=t.id LEFT JOIN users AS u ON r.user_id=u.id WHERE MATCH(r.message) AGAINST ('$searchstring' IN BOOLEAN MODE) ORDER BY score DESC");

				//-----------------------------------------------------------------
				//  Do some HTML
				//-----------------------------------------------------------------

				echo "<br>";
				echo "<table class='main' cellspacing='1' cellpadding='4'>";
				echo "<tr class='head'><td colspan='2'>{$language['Search action desc']}</td></tr>";

				//-----------------------------------------------------------------
				//  Check if we have results
				//-----------------------------------------------------------------

				if($result != FALSE)
				{
					//-----------------------------------------------------------------
					//  Do foreach
					//-----------------------------------------------------------------

					foreach($result as $idx=>$forum_reply)
					{
						if(isset($is_moderator) || isset($is_administrator) || isset($is_webmaster)){}
						else if($forum_reply['type'] == "3") continue;

						if(isset($forum_reply['uid'])) echo "<tr class='bottom'><td><table width='100%' cellspacing='0' cellpadding='0'><tr class='empty'><td width='50%' align='left'><span style='font-size: 12px; font-weight: bold;'><a href='$PHP_SELF?n=modules/users&a=1&d={$forum_reply['user_id']}'>{$forum_reply['user_user']}</a></span></td>";
						else echo "<tr class='bottom'><td><table width='100%' cellspacing='0' cellpadding='0'><tr class='empty'><td width='50%' align='left'><span style='font-size: 12px; font-weight: bold;'><a href='mailto:{$forum_reply['user_email']}'>{$forum_reply['user_user']}</a></span></td>";

						echo "<td width='50%' align='right'><b><font color='#848484'><img src='templates/$template/images/buttons/date.gif' border='0' alt='{$language['Forum add reply desc10']}'> ".today_yesterday($forum_reply['date'])."</font></b></td></tr></table></td></tr>";
						echo "<tr valign='top' class='con1'><td colspan='2'>";
						echo "<table width='100%' class='main' cellspacing='1' cellpadding='4'><tr><td class='con3'><a href='$PHP_SELF?n=modules/forum'>{$config['forum_name']}</a> &raquo; <a href='$PHP_SELF?n=modules/forum&a=1&d={$forum_reply['cid']}'>{$forum_reply['cname']}</a> &raquo; <a href='$PHP_SELF?n=modules/forum&a=2&d={$forum_reply['cid']}&o={$forum_reply['sid']}'>{$forum_reply['sname']}</a> &raquo; <a href='$PHP_SELF?n=modules/forum&a=6&d={$forum_reply['rid']}'>{$forum_reply['tname']}</a></td></tr></table><table><tr><td></td></tr></table>".parse_message($forum_reply['message'])."</td></tr>";
					}
				}

				//-----------------------------------------------------------------
				//  If we don't have results show error
				//-----------------------------------------------------------------

				else echo "<tr class='con1'><td colspan='6'>{$language['Search action desc']}</td></tr>";

				//-----------------------------------------------------------------
				//  Finish the HTML, close the table
				//-----------------------------------------------------------------

				echo "</table>";

			break;

			//-----------------------------------------------------------------
			//  Search in users username
			//-----------------------------------------------------------------

			case 3:

				//-----------------------------------------------------------------
				//  Get the search results
				//-----------------------------------------------------------------

				$result = $db->getTable("SELECT hideemail, email, id, flag, location, username, authorization, MATCH(username) AGAINST ('$searchstring' IN BOOLEAN MODE) AS score FROM users WHERE MATCH(username) AGAINST ('$searchstring' IN BOOLEAN MODE) ORDER BY score DESC");

				//-----------------------------------------------------------------
				//  Do some HTML
				//-----------------------------------------------------------------

				echo "<br>";
				echo "<table class='main' cellspacing='1' cellpadding='4'>";
				echo "<col width='1%'><col width='30%'><col width='35%'><col width='30%'>";
				echo "<tr class='head'><td colspan='4'>{$language['Search action desc']}</td></tr>";
				echo "<tr class='bottom'><td>{$language['Users view title']}</td><td>{$language['Users view title2']}</td><td>{$language['Users view title4']}</td><td>{$language['Users view title5']}</td></tr>";

				//-----------------------------------------------------------------
				//  Check if we have results
				//-----------------------------------------------------------------

				if($result != FALSE)
				{
					//-----------------------------------------------------------------
					//  Do foreach
					//-----------------------------------------------------------------

					foreach($result as $idx=>$user)
					{
						//-----------------------------------------------------------------
						//  Hide email if option says so
						//-----------------------------------------------------------------

						if($user['hideemail'] == 1 && !isset($is_administrator) && !isset($is_webmaster)) $email = "<span class='inv'>{$language['Users view desc4']}</span>";
						elseif($user['hideemail'] == 1) $email = "<a href='mailto:{$user['email']}'>{$user['email']}</a> <span class='inv'>({$language['Users view desc4']})</span>";
						else $email = "<a href='mailto:{$user['email']}'>{$user['email']}</a>";

						//-----------------------------------------------------------------
						//  Do more HTML
						//-----------------------------------------------------------------

						echo "<tr class='".row2color($idx)."' onMouseOver=this.className='con3' onMouseOut=this.className='".row2color($idx)."' onclick=window.location.href='$PHP_SELF?n=modules/users&a=1&d={$user['id']}'>";
						echo "<td align='center'>".find_flag($user['flag'],$user['location'])."</td>";
						echo "<td><a href='$PHP_SELF?n=modules/users&a=1&d={$user['id']}'>{$user['username']}</a></td>";
						echo "<td>$email</td>";
						echo "<td>".find_authorization($user['authorization'])."</td>";
						echo "</tr>";
					}
				}

				//-----------------------------------------------------------------
				//  If we don't have results show error
				//-----------------------------------------------------------------

				else echo "<tr class='con1'><td colspan='6'>{$language['Search action desc3']}</td></tr>";

				//-----------------------------------------------------------------
				//  Finish the HTML, close the table
				//-----------------------------------------------------------------

				echo "</table>";

			break;
		}

	break;

	//-----------------------------------------------------------------
	//  Show only form
	//-----------------------------------------------------------------

	default: searchForm();
}
?>